package se.kernel;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class UtilidadEncriptacion
{
    public static String encriptarPassword( String password )
    {
        return convertirAString( cifrar( password ) );
    }

    private static byte[] cifrar( String password )
    {
        MessageDigest d = null;
        try
        {
            d = MessageDigest.getInstance( "SHA-1" );
            d.reset( );
            d.update( password.getBytes( ) );
        }
        catch( NoSuchAlgorithmException e )
        {
            e.printStackTrace( );
        }
        return d.digest( );
    }

    private static String convertirAString( byte[] b )
    {
        StringBuffer sb = new StringBuffer( b.length * 2 );
        for( int i = 0; i < b.length; i++ )
        {
            int v = b[ i ] & 0xff;
            if( v < 16 )
            {
                sb.append( '0' );
            }
            sb.append( Integer.toHexString( v ) );
        }
        return sb.toString( ).toUpperCase( );
    }
}
